package gazzera.node.core.plugins.shareplugin.db;

import static com.sleepycat.persist.model.Relationship.*;
import gazzera.node.core.plugins.download.*;
import gazzera.utils.*;

import com.sleepycat.persist.model.*;

@Entity
public class FileInfo
{
	@PrimaryKey
	public String fullname;
	public long timestamp;
	public long size;
	@SecondaryKey(relate=MANY_TO_ONE)
	public Hash hash;
	
	public FileInfo()
	{
		
	}
	
	public FileInfo(String name, long timestamp, long size,
			Hash hash) {
		super();
		this.fullname = name;
		this.timestamp = timestamp;
		this.size = size;
		this.hash = hash;
	}
	
	public String getFullName()
	{
		return fullname;
	}
	@Deprecated
	public String getSingleName()
	{
		throw new RuntimeException("Unimplemented");
	}
	
	public int getChunkCount()
	{
		int mod = (int)(size % Download.CHUNK_SIZE);
		int num = (int)(size / Download.CHUNK_SIZE);
		if(mod>0) num++;
		return num;
	}
	
	public int getChunkSize(int chunkIdx)
	{
		int mod = (int)(size % Download.CHUNK_SIZE);
		int num = (int)(size / Download.CHUNK_SIZE);
		
		if(chunkIdx<num)
		{
			return Download.CHUNK_SIZE;
		}
		else
		{
			return mod;
		}
	}
	
}
